package cn.itcast.web.exception;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * springmvc全局异常，只需要在容器中存在HandlerExceptionResolver类型的对象，
 * 就可有捕获到controller中的异常
 */
@Component
public class CustomException implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request,
                                         HttpServletResponse response, Object handler, Exception ex) {
        // 打印异常到控制台
        ex.printStackTrace();
        ModelAndView mv = new ModelAndView("error");
        mv.addObject("errorMsg","系统忙，请稍后再试！");
        return mv;
    }
}
